Skip to content

Conversation

@Manonox
Copy link

@Manonox Manonox commented Jan 8, 2026

Fixes: Facepunch/sbox-issues#9813
(Look there for details)

There's also a new test:

[TestMethod]
public void ObeyRecursiveGenericConstraints()
{
	var tl = new Sandbox.Internal.TypeLibrary();
	tl.AddAssembly( ThisAssembly, true );

	var goodboy = tl.GetType( typeof( ConstraintBreaker2<> ) )
								.CreateGeneric<ConstraintBreaker2TypeErased>( [typeof( ConstraintBreaker2Inner )] );
	Assert.IsNotNull( goodboy );

	var badboy = tl.GetType( typeof( ConstraintBreaker2<> ) )
								.CreateGeneric<ConstraintBreaker2TypeErased>( [typeof( TypeWrapper )] );
	Assert.IsNull( badboy );
}

public class ConstraintBreaker2Inner :
	IConstraintBreaker2<
		ConstraintBreaker2Inner,
		ConstraintBreaker2Inner,
		ConstraintBreaker2Inner
	>
{
	public void DoSomething( ConstraintBreaker2Inner value )
	{

	}

	public void DoSomething2( ConstraintBreaker2Inner value )
	{

	}

	public void DoSomething3( ConstraintBreaker2Inner value )
	{

	}
}

public class ConstraintBreaker2TypeErased { }

[Expose]
public class ConstraintBreaker2<T> : ConstraintBreaker2TypeErased where T : IConstraintBreaker2<T, T, T>
{
	public void DoSomething( T value )
	{
		value.DoSomething( value );
		value.DoSomething2( value );
		value.DoSomething3( value );
	}
}

@Manonox Manonox force-pushed the make-generic-type-fix branch from 837ed27 to db79fbb Compare January 11, 2026 09:05
@handsomematt handsomematt requested a review from Copilot January 12, 2026 18:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes recursive generic type constraint validation in TypeLibrary.MakeGenericType by implementing proper type parameter substitution when checking constraints. Previously, the method failed to validate constraints correctly when generic parameters appeared recursively within their own constraint definitions (e.g., where T : IConstraintBreaker2<T, T, T>).

Changes:

  • Added type parameter substitution logic to replace generic parameters with their concrete type arguments when validating constraints
  • Added test coverage for recursive generic constraint validation with both valid and invalid type arguments

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
engine/Sandbox.Reflection/TypeLibrary/TypeDescription.cs Implements recursive type substitution in MakeGenericType to properly validate generic constraints
engine/Sandbox.Test.Unit/Reflection/TypeLibraryTest.cs Adds test cases and supporting types to verify recursive generic constraint validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@handsomematt handsomematt added the triaged triaged pull-requests are replicated on the internal sbox repo label Jan 13, 2026
Copy link
Author

@Manonox Manonox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fairly good explanation from le bot
Changes - meh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triaged triaged pull-requests are replicated on the internal sbox repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeLibrary: MakeGenericType doesn't work correctly with generic parameter constraints

2 participants